package com.jeesite.modules.kj.user.service.support;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Extend;
import com.jeesite.modules.kj.user.dao.ExtendUserDao;
import com.jeesite.modules.kj.user.service.UserExtendService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.support.UserServiceSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceExtendSupport implements UserExtendService {
    @Autowired
    ExtendUserDao extendUserDao;

    @Override
    public User getByOneCode(User user) {
        User oneCodeUser = extendUserDao.getByOneCode(user);
        return oneCodeUser;
    }

    @Override
    public String checkMemberLoginCode(String oldLoginCode, String loginCode) {
        User u = new User();
        u.setUserType(User.USER_TYPE_MEMBER);
        Extend extend = new Extend();
        extend.setExtendS1(loginCode);
        u.setExtend(extend);
        if (loginCode != null && loginCode.equals(oldLoginCode)) {
            return Global.TRUE;
        } else {
            return loginCode != null && this.extendUserDao.getByExtendS1(u) == null ? Global.TRUE : Global.FALSE;
        }

    }

    @Override
    public String checkMemberMobile(String oldLoginCode, String mobile) {
        User u = new User();
        u.setUserType(User.USER_TYPE_MEMBER);
        u.setLoginCode(oldLoginCode);
        u.setMobile(mobile);
        if (mobile == null) {
            return Global.TRUE;
        } else {
            User mobileUser = this.extendUserDao.getByMobile(u);
            return mobile != null &&  (mobileUser == null || mobileUser.getLoginCode().equals(oldLoginCode))  ? Global.TRUE : Global.FALSE;
        }
    }

    @Override
    public String checkMemberEmail(String oldLoginCode, String email) {
        User u = new User();
        u.setUserType(User.USER_TYPE_MEMBER);
        u.setLoginCode(oldLoginCode);
        u.setEmail(email);
        if (email == null) {
            return Global.TRUE;
        } else {
            User emailUser = this.extendUserDao.getByEmail(u);
            return email != null && (emailUser == null || emailUser.getLoginCode().equals(oldLoginCode)) ? Global.TRUE : Global.FALSE;
        }
    }
}
